package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import db.UpelaKud;

public class GehituUpela extends JDialog {

	private Upela guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel upelalbl;
	private JTextField upelatxt;
	private JLabel edukieralbl;
	private JTextField edukieratxt;
	private JButton gordebtn;
	private Vector<Integer> balioak;

	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			GehituUpela dialog = new GehituUpela(new Upela(null, MODALA),
					MODALA);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public GehituUpela(Upela guraso, Boolean mODALA) {
		
		this.guraso = guraso;

		setBounds(100, 100, 300, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("GEHITU UPELA");
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		upelalbl = new JLabel("UPELA ZENBAKIA");
		upelalbl.setBounds(70, 40, 120, 25);
		contentPanel.add(upelalbl);

		upelatxt = new JTextField();
		upelatxt.setBounds(70, 80, 120, 25);
		contentPanel.add(upelatxt);

		edukieralbl = new JLabel("EDUKIERA");
		edukieralbl.setBounds(70, 110, 120, 25);
		contentPanel.add(edukieralbl);

		edukieratxt = new JTextField();
		edukieratxt.setBounds(70, 150, 120, 25);
		contentPanel.add(edukieratxt);

		gordebtn = new JButton("GORDE");
		gordebtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				

				Vector<Integer> emaitza = GehituUpela.this.konprobatudena();
				if (emaitza.elementAt(0) == 1 && emaitza.elementAt(1) == 1) {
					JOptionPane.showMessageDialog(GehituUpela.this,
							"Bete eremuak, ZENBAKIAK sartu!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				} else if (emaitza.elementAt(0) == 1) {
					JOptionPane.showMessageDialog(GehituUpela.this,
							"Upela zenbakia sartu, ZENBAKIA!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				} else if (emaitza.elementAt(1) == 1) {
					JOptionPane.showMessageDialog(GehituUpela.this,
							"Upelaren edukiera sartu, ZENBAKIA!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				}

				else {
					int upela = balioak.elementAt(0);
					int edukiera = balioak.elementAt(1);
					boolean badago = GehituUpela.this.badagoUpela(upela);
					if (badago == true) {
						JOptionPane.showMessageDialog(GehituUpela.this,
								"Upela hau badago!", "KONTUZ",
								JOptionPane.WARNING_MESSAGE);
					} else {
						int konfirmatu = JOptionPane.showConfirmDialog(
								GehituUpela.this, "Gorde nahi duzu?");
						if (JOptionPane.OK_OPTION == konfirmatu) {
							GehituUpela.this.gorde(upela, edukiera);

							GehituUpela.this.dispose();
							GehituUpela.this.guraso.ezabatutaula();
							GehituUpela.this.guraso.bistaratu();
						}
						else if (JOptionPane.NO_OPTION==konfirmatu){
							GehituUpela.this.dispose();

						}
					}
				}

			}
		});
		gordebtn.setBounds(70, 210, 120, 25);
		contentPanel.add(gordebtn);

	}

	protected boolean badagoUpela(int upela) {
		
		int emaitza = UpelaKud.badagoUpela(upela);
		if (emaitza == 0) {
			return false;
		} else {
			return true;
		}
	}

	protected Vector<Integer> konprobatudena() {
		
		balioak = new Vector<Integer>();

		Vector<Integer> emaitza = new Vector<Integer>();

		try {
			String katea = upelatxt.getText();
			int balioa = Integer.parseInt(katea);
			emaitza.add(0, 0);
			balioak.add(0, balioa);
		} catch (NumberFormatException e) {
			emaitza.add(0, 1);
			balioak.add(0, 0);
		}
		try {
			String katea1 = edukieratxt.getText();
			int balioa1 = Integer.parseInt(katea1);
			emaitza.add(1, 0);
			balioak.add(1, balioa1);
		} catch (NumberFormatException e) {
			emaitza.add(1, 1);

		}
		return emaitza;
	}

	protected void gorde(int upela, int edukiera) {
		

		UpelaKud.gehituUpela(upela, edukiera);
	}

}
